﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MinhTam.Objects;
using System.Data;
using MinhTam.DataAccess.Web;

public partial class AdminSiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

       // Page.Form.Attributes.Add("enctype", "multipart/form-data");
        this.Page.Title = "Quản trị - " + HelpConstEnum.SHORT_TITLE;
        InitMenu();
        bool isLogin = false;
        if (this.Page.ToString() != "ASP.admin_login_aspx")
        {
            if (HelpUtil.IsNotLogin(this.Page)) return;
            isLogin = true;
        }
        else
        {
            if (Page.Session[HelpConstEnum.ADMIN_IS_LOGIN_SESSION] != null && Page.Session[HelpConstEnum.ADMIN_IS_LOGIN_SESSION].ToString() == "Y" && UserLogin.IsLogin(Page))
                isLogin = true;          
        }
        if (isLogin)
        {
            LOG_IN_SESSION login = UserLogin.GetCurentLogin(this.Page);
            lblUserName.Text = login.FULL_NAME;
            try
            {
                lblDateTime.Text = login.CLIENT_DATE == "" ? "" : DateTime.Parse(login.CLIENT_DATE).ToString("dd/MM/yyyy HH:mm");
            }
            catch
            {
                lblDateTime.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
            }
        }
        else
        {
            lblDateTiemTitle.Visible = false;
            lblDateTime.Visible = false;
            lblUserName.Visible = false;
            lblWelcome.Visible = false;
            btnLogout.Visible = false;
            NavigationMenu.Visible = false;
            btnChangePass.Visible = false;
        }
      
       
            
    }
    private void InitMenu()
    {

        MenuItem item = new MenuItem();
        item.Selectable = false;
        NavigationMenu.Items.Add(item);
        
        decimal total = 0; 
        DataTable dt = LOAI_BAI_VIET.GetTableAll(LOAI_BAI_VIET.cl_SU_DUNG + "='Y'", LOAI_BAI_VIET.cl_SAP_XEP);
       if (dt != null && dt.Rows.Count > 0)
       {
           decimal count = 0;
          
           foreach (DataRow r in dt.Rows)
           {
               string tenloai = r[LOAI_BAI_VIET.cl_TEN_LOAI].ToString();
               if (HelpUtil.IsLoaiBaiVietTuKH(r[LOAI_BAI_VIET.cl_LOAI_BAI_VIET_ID].ToString()))
               {
                   string sql = "select count(bai_viet_id) dem from bai_viet where isnull(da_doc,'N')  <>'Y' and loai_bai_viet=" + r[LOAI_BAI_VIET.cl_LOAI_BAI_VIET_ID];
                   DataTable dtC = SQLConnectWeb.GetTable(sql);
                   if (dtC != null && dtC.Rows.Count > 0)
                   {
                       count = HelpUtil.ParseDecimal(dtC.Rows[0]["dem"]);
                       total += count;
                       tenloai += count > 0 ? " (" + count + ")" : "";
                   }
               }
              MenuItem m=new MenuItem(tenloai, "", "", "QuanLyBaiViet.aspx?typeid=" + r[LOAI_BAI_VIET.cl_LOAI_BAI_VIET_ID]);               
               item.ChildItems.Add(m);
               //m.ChildItems.Add(new MenuItem("Tạo mới", "", "", "BaiViet.aspx?typeid=" + r[LOAI_BAI_VIET.cl_LOAI_BAI_VIET_ID]));
           }
       }
       item.Text = "QL Bài viết" + (total > 0 ? " (" + total + ")" : "");
       NavigationMenu.Items.Add(new MenuItem("QL Văn phòng", "", "",  "QuanLyVanPhong.aspx"));
       NavigationMenu.Items.Add(new MenuItem("QL Tuyến đường", "", "", "QuanLyTuyenDuong.aspx"));
       NavigationMenu.Items.Add(new MenuItem("QL Bảng giá", "", "", "QuanLyBangGiaVe.aspx"));
       NavigationMenu.Items.Add(new MenuItem("QL Album ảnh", "", "", "QuanLyAlbum.aspx"));
        MenuItem itemDM = new MenuItem("QL Danh mục");
       itemDM.Selectable = false;
       NavigationMenu.Items.Add(itemDM);
       itemDM.ChildItems.Add(new MenuItem("Địa điểm du lịch", "", "", "DMDiaDiemDuLich.aspx"));
       itemDM.ChildItems.Add(new MenuItem("Địa điểm đưa rước miễn phí", "", "", "DMDiaDiemDuaRuoc.aspx"));
       itemDM.ChildItems.Add(new MenuItem("Khách sạn", "", "", "DMKhachSan.aspx"));
       itemDM.ChildItems.Add(new MenuItem("Tỉnh/thành", "", "", "DMTinhThanh.aspx"));
       MenuItem itemTC= new MenuItem("Tùy chọn");
       itemTC.Selectable = false;
       NavigationMenu.Items.Add(itemTC);
       itemTC.ChildItems.Add(new MenuItem("Ảnh Slide show", "", "", "OptionSlideShow.aspx"));
       
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        UserLogin.RemoveLogin(Page);
        Session.Remove(HelpConstEnum.ADMIN_IS_LOGIN_SESSION);
        Response.Redirect("Login.aspx");
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
       
        Response.Redirect("ChangePassword.aspx");
    }
}
